package com.cnswhy.cloud.server.core.service.shop.impl;


import com.cnswhy.cloud.common.core.base.service.SuperServiceImpl;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.core.constant.RedisConstant;
import com.cnswhy.cloud.server.core.dao.ShopOrderMapper;
import com.cnswhy.cloud.server.core.dto.*;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.service.ShopOrderDetailService;
import com.cnswhy.cloud.server.core.service.ShopOrderReceiverService;
import com.cnswhy.cloud.server.core.service.shop.ShopOrderService;
import com.cnswhy.cloud.server.core.service.shop.ShopProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 商品订单表
 * </p>
 *
 * @author cnswhy
 * @date 2020-09-30
 */
@Slf4j
@Service

public class ShopOrderServiceImpl extends SuperServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {
    @Autowired
    RedisService redisService;
    @Autowired
    ShopProductService shopProductService;
    @Autowired
    ShopOrderReceiverService shopOrderReceiverService;
    @Autowired
    ShopOrderDetailService shopOrderDetailService;
    @Override
    public ShopOrder queryById(Long id)
    {
        ShopOrder queryById = getBaseMapper().queryById(id);
        String prefix = (String) redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_PREFIX);
        List<ShopOrderDetail> detailList = shopOrderDetailService.list(Wraps.<ShopOrderDetail>lbQ().eq(com.cnswhy.cloud.server.core.entity.ShopOrderDetail::getOrderId, queryById.getId()));
        for (ShopOrderDetail detail : detailList)
        {
            detail.setProductImg(prefix + detail.getProductImg());
        }
        return queryById;
    }
    @Override
    public ShopOrder queryByPayOrderNo(String payOrderNum)
    {
        return getBaseMapper().queryByPayOrderNo(payOrderNum);
    }
    @Override
    public ShopOrder queryByOrderNo(String orderNo)
    {
        return getBaseMapper().queryByOrderNo(orderNo);
    }
    @Override
    public void insert(ShopOrder entity) {
        getBaseMapper().insert1(entity);
    }
    @Override
    public void update(ShopOrder entity) {
        getBaseMapper().update1(entity);

    }

    @Override
    public BuyerCart getBuyerCart(List<BuyerOperateBo> list, CustomerReceiver receiver)
    {
        BuyerCart buyerCart = new BuyerCart();
        for (BuyerOperateBo bo : list)
        {
            // 组装商品项
            BuyerItem buyerItem = new BuyerItem();
            ShopProductSku sku = shopProductService.queryShopProductSku(bo.getStockId());
            if (StringUtils.isNotBlank(sku.getUrl())) {
                sku.setImgUrl(sku.getUrl());
            }else if (StringUtils.isNotBlank(sku.getImgUrl())) {
                sku.setImgUrl(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_PREFIX) + sku.getImgUrl());
            }else{
                sku.setImgUrl(sku.getThumbnailImg());
            }
            /*sku.setImgUrl(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_PREFIX)
                    + (StringUtils.isNotBlank(sku.getImgUrl()) ? sku.getImgUrl() : sku.getThumbnailImg()));*/
            sku.setTransportType(bo.getTransportType());
            buyerItem.setSku(sku);
            buyerItem.setNum(bo.getNum());

            // 组装商家项
            //Merchant merchant = merchantService.getById(bo.getMerchantId());
            BuyerMerchant buyerMerchant = new BuyerMerchant();
            //buyerMerchant.setMerchantId(merchant.getId());
            //buyerMerchant.setMerchantName(merchant.getMerchantName());
            buyerMerchant.addItem(buyerItem);

            // 计算运费
            this.countFreightPrice(buyerMerchant, receiver);

            // 放入购物车对象中
            buyerCart.addMerchant(buyerMerchant);
        }

        return buyerCart;
    }

    /**
     * 描述：计算商品运费 作者：张润泽 时间：2019年9月2日 下午7:59:45
     *
     * @param merchant
     * @param receiver
     */
    private void countFreightPrice(BuyerMerchant merchant, CustomerReceiver receiver)
    {
        // 查询商品详情
        ShopProduct product = shopProductService.getById(merchant.getItems().get(0).getSku().getProductId());

        for (BuyerItem item : merchant.getItems())
        {
            // 当前购物项运费
            BigDecimal subPrice = BigDecimal.ZERO;
            // 查询库存详情
            // ProductStock stock = shopProductDao.queryStockById(item.getSku().getStockId());
            // totalNum = item.getNum();
            // totalWeight = stock.getWeight() * item.getNum();
            // totalPrice = totalPrice.add(stock.getSellPrice().multiply(new BigDecimal(item.getNum())));

            // 若是固定金额，则直接取价格
            if (product.getFreightType() == 1)
            {
                subPrice = product.getFreightPrice();
            }
            // 设置当前购物项运费
            item.setFreightPrice(subPrice);
        }
    }

    @Override
    public void insertReceiver(CustomerReceiver invoice,Long orderId) {
        //getBaseMapper().insertReceiver(invoice);
        ShopOrderReceiver shopOrderReceiver = ShopOrderReceiver.builder()
                .address(invoice.getAddress())
                .area(invoice.getArea())
                .mobile(invoice.getMobile())
                .receiver(invoice.getReceiver())
                //.orderId(invoice.getOrderId())
                .orderId(orderId)
                .zipCode(invoice.getZipCode()).build();
        shopOrderReceiverService.save(shopOrderReceiver);
    }

    @Override
    public List<ShopOrder> pageList(ShopOrder shopOrder) {
        return getBaseMapper().pageList(shopOrder);
    }
}
